﻿using log4net;
using log4net.Config;
using System;
using System.IO;

namespace CommonNet48.LogUtils.Log4net
{
    /// <summary>
    /// Log4net日志操作帮助类
    /// </summary>
    public class Log4
    {
        /// <summary>
        /// Log4net配置文件路径（可根据实际情况修改）
        /// </summary>
        private static readonly string _ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "log4net.cfg.xml");

        /// <summary>
        /// 静态构造
        /// </summary>
        static Log4()
        {
            XmlConfigurator.Configure(new FileInfo(_ConfigFilePath));//加载Log4net配置文件
        }

        /// <summary>
        /// 日志操作接口
        /// </summary>
        private ILog loger = null;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="type">操作日志类的类型</param>
        public Log4(Type type)
        {
            loger = LogManager.GetLogger(type);
        }

        /// <summary>
        /// 异常日志
        /// </summary>
        /// <param name="msg">异常输出信息</param>
        /// <param name="ex">异常对象</param>
        public void Error(string msg, Exception ex)
        {
            Console.WriteLine(msg);
            loger.Error(msg, ex);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <param name="msg">告警输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Warn(string msg, Exception ex = null)
        {
            Console.WriteLine(msg);
            loger.Warn(msg, ex);
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="msg">提示输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Info(string msg, Exception ex = null)
        {
            Console.WriteLine(msg, ex);
            loger.Info(msg);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="msg">调试输出信息</param>
        /// <param name="ex">异常对象，没有异常可以不传，默认为null</param>
        public void Debug(string msg, Exception ex = null)
        {
            Console.WriteLine(msg, ex);
            loger.Debug(msg);
        }
    }
}
